package com.gao.api.gate.quick.core.registry;

import java.util.Collection;

/**
 * <p></p>
 *
 * @author gaoshuangwen
 * @since 2022/01/12 13:06
 */
public abstract class SimpleRegistry<Node> implements Registry<Node> {

    private final Collection<Node> registry;

    public SimpleRegistry(Collection<Node> registry) {
        this.registry = registry;
    }

    @Override
    public  boolean addNode(Node node) {

        boolean flag = registry.add(node);
        if(flag){
            onChanged(RegistryEvent.add);
        }
        return flag;
    }

    @Override
    public  boolean addAll(Collection<Node> nodes) {
        boolean flag = registry.addAll(nodes);
        if(flag){
            onChanged(RegistryEvent.add);
        }
        return flag;
    }

    @Override
    public  boolean remove(Node node) {
        boolean flag = registry.remove(node);
        if(flag ){
            onChanged(RegistryEvent.remove);
        }
        return flag;
    }

    @Override
    public void clear() {
        registry.clear();
        onChanged(RegistryEvent.clear);
    }

    @Override
    public Collection<Node> getNodes() {
        return registry;
    }

    public abstract void onChanged(RegistryEvent event);


}
